package com.qixin.modules.eaBasic.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.qixin.modules.eaBasic.entity.ActivitiTask;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.service.IEaTaskService;
import com.qixin.modules.ea.vo.TaskPageVo;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.entity.ActZprocess;
import org.jeecg.modules.activiti.entity.TaskVo;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.activiti.service.Impl.ActZprocessServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: ea_task
 * @Author: qixin-boot
 * @Date:   2021-09-18
 * @Version: V1.0
 */
@Service
public class EaTaskServiceImpl extends ServiceImpl<EaTaskMapper, EaTask> implements IEaTaskService {
    @Autowired
    EaTaskMapper eaTaskMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private ActBusinessServiceImpl actBusinessService;
    @Autowired
    private ChangeIdToNameUtil changeIdToNameUtil;

    @Override
    public Map<String, Object> list(TaskPageVo taskPageVo) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Integer page = taskPageVo.getPage() != null ? taskPageVo.getPage() : 1;
        Integer limit = taskPageVo.getLimit() != null ? taskPageVo.getLimit() : 500;
        String keyword = EmptyUtil.checkNull(taskPageVo.getKeyword());  //关键字
        taskPageVo.setPage((page - 1) * limit);
        taskPageVo.setLimit(limit);
        taskPageVo.setUserid(loginUser.getId());
        if(!EmptyUtil.checkNull(keyword).equals("")){
            taskPageVo.setKeyword("%"+keyword+"%");
        }
        //List<EaTask> list = eaTaskMapper.selectEaTasks(taskPageVo);
        List<ActivitiTask> list = new ArrayList<ActivitiTask>();
        String dbStatus = taskPageVo.getDb_status() != null ? taskPageVo.getDb_status() : "0";
        if(dbStatus.equals("0")){
            list = eaTaskMapper.selectEaDbTasks(taskPageVo);
        }else{
            list = eaTaskMapper.selectEaYbTasks(taskPageVo);
        }

        for (int i = 0; i < list.size(); i++) {
            ActivitiTask activitiTask = list.get(i);
            //key
            String key = activitiTask.getTaskDefKey();
            activitiTask.setKey(key);
            //创建时间
            String startTime = EmptyUtil.checkNull(activitiTask.getCreateTime());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            try {
                if(!startTime.equals("")){
                    date = simpleDateFormat.parse(startTime);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

            activitiTask.setCreateTime(simpleDateFormat.format(date));
        }

        //查询数据条数
        int count = 0;
        taskPageVo.setPage(0);
        taskPageVo.setLimit(0);
        if(taskPageVo.getDb_status().equals("0")){
            count = eaTaskMapper.selectEaDbTasksCount(taskPageVo);
        }else{
            count = eaTaskMapper.selectEaYbTasksCount(taskPageVo);
        }
        Map<String,Object> returnMap = new HashMap<String,Object>();
        returnMap.put("records",list);
        returnMap.put("total",count);
        return returnMap;
    }

}
